home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ARASAN_S.ZIP / MOVEARR.CPP < prev    next >
C/C++ Source or Header  |  1994-07-03  |  3KB  |  105 lines

  1. // Copyright 1993 by Jon Dart.  All Rights Reserved.
  2.  
  3. #include "movearr.h"
  4.  
  5. const Initial_Move_Array_Size = 100;  // initial array size
  6.                       // (expandable if necessary)
  7.  
  8. Move_Record::Move_Record(const Board &board, const ExtendedMove &move)
  9. : my_move(move),my_hashcode(board.HashCode())
  10. {
  11.   my_caststat[White] = board.CastleStatus(White);
  12.   my_caststat[Black] = board.CastleStatus(Black);
  13. }
  14.  
  15. Move_Record::Move_Record()
  16. : my_move(ExtendedMove()),my_hashcode(0)
  17. {
  18.   my_caststat[White] = Board::CanCastleEitherSide;
  19.   my_caststat[Black] = Board::CanCastleEitherSide;
  20. }
  21.  
  22. int Move_Record::operator == ( const Move_Record &l ) const
  23. {
  24.   return my_hashcode == l.my_hashcode &&
  25.   my_caststat[White] == l.my_caststat[White] &&
  26.   my_caststat[Black] == l.my_caststat[Black];
  27. }
  28.  
  29. int Move_Record::operator != ( const Move_Record &l ) const
  30. {
  31.   return my_hashcode != l.my_hashcode ||
  32.   my_caststat[White] != l.my_caststat[White] ||
  33.   my_caststat[Black] != l.my_caststat[Black];
  34. }
  35.  
  36. Move_Array::Move_Array() : Array<Move_Record>(Initial_Move_Array_Size,False)
  37. {
  38.     for (int i=0; i<Rep_Table_Size;i++)
  39.       rep_table[i] = 0;
  40. }
  41.          
  42. Move_Array::~Move_Array()
  43. {
  44. }
  45.  
  46. void Move_Array::add_move( const Board &board, const ExtendedMove &emove )
  47. {
  48.     Move_Record entry( board, emove );
  49.     *this += entry;
  50.     rep_table[board.HashCode() % Rep_Table_Size]++;
  51. }
  52.  
  53. void Move_Array::remove_move()
  54. {
  55.     if (num_moves() == 0)
  56.         return;
  57.     Move_Record &entry = (*this)[num_moves()-1];
  58.     rep_table[entry.hashcode() % Rep_Table_Size]--;
  59.     resize(size()-1);
  60. }
  61.  
  62. const ExtendedMove &Move_Array::move( const unsigned n )
  63. {
  64.     return (*this)[n].move();
  65. }
  66.     
  67. int Move_Array::rep_count( const Board &board) const
  68. {
  69.     // we do a quick check first to see if any entry has the same
  70.     // lower 7 bits in the hash code.
  71.     if (num_moves() == 0 ||
  72.         rep_table[board.HashCode() % Rep_Table_Size] == 0)
  73.        return 0;
  74.     int n = num_moves()-1;
  75.     int count = 0;
  76.     Move_Record e(board,ExtendedMove());
  77.     for (int i = n ; i >= 0; --i )
  78.     {
  79.        Move_Record &cur_entry = (*this)[i];
  80.        if (cur_entry == e)
  81.           count++;
  82.        const ExtendedMove &cur_move = cur_entry.move();
  83.        if (!cur_move.Capture().IsEmpty() ||
  84.             cur_move.PieceMoved().Type() == Piece::Pawn ||
  85.         cur_move.Special() != ExtendedMove::Normal)
  86.         // irExtended move
  87.           break;
  88.     }
  89.     return count;
  90. }    
  91.  
  92. void Move_Array::clear()
  93. {
  94.      resize(0);
  95. }
  96.  
  97. unsigned Move_Array::num_moves(const ColorType side)
  98. {
  99.     if (side == White)
  100.         return num_moves() ? num_moves()/2 + 1 : 0;
  101.     else
  102.         return num_moves() ? (num_moves()-1)/2 : 0;
  103. }
  104.  
  105.